# -*- coding: utf-8 -*- 
#!/usr/bin/python2.7
"""MVC的cotroler,url映射后会生成对象，并且调用GET或者POST返回"""

__authors__ = [
  '"BigYuki" <sheyuqi@gmail.com>',
] 
from core.services.FormService import FormServiceFactory
from core.services.DataService import DataServiceFactory
from core.paginations import Pagination,GaePagination
from core.pages import BlogPage

class PageFactory(object):
    @staticmethod
    def IndexPageInstance(*args,**kw):
        return IndexPage(*args,**kw)
    @staticmethod
    def ArticlePageInstance(*args,**kw):
        return ArticlePage(*args,**kw)
    @staticmethod
    def CategoryArchivePageInstance(*args,**kw):
        return CategoryArchivePage(*args,**kw)
    @staticmethod
    def DateArchivePageInstance(*args,**kw):
        return DateArchivePage(*args,**kw)
    @staticmethod
    def TagArchivePageInstance(*args,**kw):
        return TagArchivePage(*args,**kw)
    @staticmethod
    def FeedPageInstance(*args,**kw):
        return FeedPage(*args,**kw)
    @staticmethod
    def NofoundPageInstance(*args,**kw):
        return NofoundPage(*args,**kw)
    
class BasicPage(BlogPage):
    """BasicPage获取基本页面数据。"""
    def __init__(self):
        BlogPage.__init__(self)
        
        self.blog=DataServiceFactory.BlogServiceInstance().get()
        self.categoryList=DataServiceFactory.CategroyServiceInstance().all()
        self.archiveList=DataServiceFactory.ArchiveServiceInstance().all()
        self.tagList=DataServiceFactory.TagServiceInstance().all()
        self.navigationList=DataServiceFactory.NavigationServiceInstance().all(position='TOP')
        self.linkList=DataServiceFactory.NavigationServiceInstance().all(position='SIDE')
        
    def getTemplateAbsPath(self):
        themeAbsDiretory='themes'
        themeFolder=DataServiceFactory.BlogServiceInstance().get().theme
        return themeAbsDiretory+'/'+themeFolder+'/'+self.templateFile

class IndexPage(BasicPage):
    """首页。"""
    def __init__(self):
        BasicPage.__init__(self)
        self.templateFile='index.html'
        self.pagination=GaePagination(
                                      DataServiceFactory.ArticleServiceInstance().all(),
                                      self.blog.pageSize
                                      )
        self.articleList=self.pagination.getResultSet()
    def getMaxPageNum(self):
        return self.pagination.getLastNum()
    def turnToPage(self,pageNum=1):
        if pageNum<=self.pagination.getLastNum() and pageNum>0:
            self.pagination.turnToPage(pageNum)
            self.articleList=self.pagination.getResultSet()

class NofoundPage(BasicPage):
    """显示404信息对应page。"""
    def __init__(self):
        BasicPage.__init__(self)
        self.templateFile='404.html'

class ArticlePage(BasicPage):
    """文章页面。"""
    def __init__(self,link):
        BasicPage.__init__(self)
        self.templateFile='article.html'
        
        self.article=DataServiceFactory.ArticleServiceInstance().get(link=link)
        self.commentList=DataServiceFactory.CommentServiceInstance().get(self.article.key().id())
        self.commentForm=FormServiceFactory.CommentNewFormServiceInstance(articleID=self.article.key().id()).get()

class FeedPage(BasicPage):
    """Atom feed。"""
    def __init__(self):
        BasicPage.__init__(self)
        self.templateFile='atom.xml'
        self.articleList=DataServiceFactory.ArticleServiceInstance().all()

class ArchivePage(IndexPage):
    """抽象Page,处理请求：分类，时间归档，标签归档。"""
    def __init__(self):
        IndexPage.__init__(self)
        self.templateFile='archive.html'
        self.archiveType=u'分类|时间|标签'
        self.archiveValue=u"对应的值"
class CategoryArchivePage(ArchivePage):
    """分类归档页面。"""
    def __init__(self,categoryName):
        ArchivePage.__init__(self)
        self.archiveType=u'分类'
        self.archiveValue=categoryName
        
        self.pagination=GaePagination(DataServiceFactory.ArticleServiceInstance().get(categoryName=categoryName),self.blog.pageSize)
        self.articleList=self.pagination.getResultSet()
class DateArchivePage(ArchivePage):
    """处理请求时间归档。"""
    def __init__(self,year,month):
        ArchivePage.__init__(self)
        self.archiveType=u'时间'
        self.archiveValue=u'%d 年 %d 月' % (year,month)
        
        self.pagination=GaePagination(DataServiceFactory.ArticleServiceInstance().get(year=year,month=month),self.blog.pageSize)
        self.articleList=self.pagination.getResultSet()
class TagArchivePage(ArchivePage):
    """处理请求标签归档。"""
    def __init__(self,tagName):
        ArchivePage.__init__(self)
        self.archiveType=u'标签'
        self.archiveValue=tagName
        
        self.pagination=Pagination(DataServiceFactory.ArticleServiceInstance().get(tagName=tagName),self.blog.pageSize)
        self.articleList=self.pagination.getResultSet()